var inquirer = require('inquirer');
 
function likesFood(aFood) {
  return function (answers) {
    return answers[aFood];
  };
}

const requireLetterAndNumber = (value) => {
    if (/\w/.test(value) && /\d/.test(value)) {
      return true;
    }
    return 'Password need to have at least a letter and a number';
};

inquirer.prompt([
    {
        type:"list",
        message:"选择一种方式",
        name:'way',
        choices: ["input","input(validate number)","confirm","list","rawlist","expand", "checkbox", "password", "editor"],
        default:"confirm",
    },
    {
        type: 'checkbox',
        message: 'Select toppings',
        name: 'toppings',
        choices: [
          new inquirer.Separator(' = The Meats = '),
          {
            name: 'Pepperoni',
          },
          {
            name: 'Ham',
          },
          {
            name: 'Ground Meat',
          },
          {
            name: 'Bacon',
          },
          new inquirer.Separator(' = The Cheeses = '),
          {
            name: 'Mozzarella',
            checked: true,
          },
          {
            name: 'Cheddar',
          },
          {
            name: 'Parmesan',
          },
          new inquirer.Separator(' = The usual ='),
          {
            name: 'Mushroom',
          },
          {
            name: 'Tomato',
          },
          new inquirer.Separator(' = The extras = '),
          {
            name: 'Pineapple',
          },
          {
            name: 'Olives',
            disabled: 'out of stock',
          },
          {
            name: 'Extra cheese',
          },
        ],
        when: function (answers) {
            return answers.way==='checkbox';
          },
        validate: function (answer) {
          if (answer.length < 1) {
            return 'You must choose at least one topping.';
          }
  
          return true;
        },
      },
      {
        type: 'expand',
        message: 'Conflict on `file.js`: ',
        name: 'overwrite',
        choices: [
          {
            key: 'y',
            name: 'Overwrite',
            value: 'overwrite',
          },
          {
            key: 'a',
            name: 'Overwrite this one and all next',
            value: 'overwrite_all',
          },
          {
            key: 'd',
            name: 'Show diff',
            value: 'diff',
          },
          new inquirer.Separator(),
          {
            key: 'x',
            name: 'Abort',
            value: 'abort',
          },
        ],
        when: function (answers) {
            return answers.way==='expand';
          },
      },
      {
        type: 'editor',
        name: 'bio',
        message: 'Please write a short bio of at least 3 lines.',
        validate: function (text) {
          if (text.split('\n').length < 3) {
            return 'Must be at least 3 lines.';
          }
    
          return true;
        },
        when: function (answers) {
            return answers.way==='editor';
          },
      },
    {
        type: 'confirm',
        name: 'bacon',
        message: 'Do you like bacon?',
        default: true,
        when: function (answers) {
            return answers.way==='confirm';
          },
      },
      {
        type: 'input',
        name: 'favorite',
        message: 'Bacon lover, what is your favorite type of bacon?',
        when: function (answers) {
          return answers.bacon;
        },
      },
      {
        type: 'input',
        name: 'favorite',
        message: 'Whew! What is your favorite type of pizza?',
        when: likesFood('pizza'),
      },
    {
      type: 'rawlist',
      name: 'theme',
      message: 'What do you want to do?',
      choices: [
        'Order a pizza',
        'Make a reservation',
        new inquirer.Separator(),
        'Ask opening hours',
        'Talk to the receptionist',
      ],
      filter: function (val) {
        return val.toLowerCase();
      },
      when: function (answers) {
        return answers.way==='rawlist';
      },
    },
    {
        type: 'input',
        name: 'phone',
        message: "What's your phone number?",
        validate: function (value) {
          var pass = value.match(
            /^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$/i
          );
          if (pass) {
            return true;
          }
    
          return 'Please enter a valid phone number';
        },
        when: function (answers) {
            return answers.way==='input';
          },
      },
      
      {
        type: 'input',
        name: 'quantity',
        message: 'How many do you need?',
        validate: function (value) {
          var valid = !isNaN(parseFloat(value));
          return valid || 'Please enter a number';
        },
        filter: Number,
        when:function(answers){
            return answers.way==='input(validate number)';
        }
      },
       
      
      
      
      {
        type: 'password',
        message: 'Enter a password',
        name: 'password1',
        validate: requireLetterAndNumber,
        when: function (answers) {
            return answers.way==='password';
          },
      },
      {
        type: 'password',
        message: 'Enter a masked password',
        name: 'password2',
        mask: '*',
        validate: requireLetterAndNumber,
        when: function (answers) {
            return answers.password1;
          },
      },
  ])
  .then((answers) => {
    var output = '输出结果为:';
    output += ' \n';
    output += ' ____  _____  ____  _____ \n';
    output += '(_  _)(  _  )(  _ \\(  _  ) \n';
    output += '  )(   )(_)(  )(_) ))(_)(  \n';
    output += ' (__) (_____)(____/(_____) \n';
    console.log(output);
    console.log(JSON.stringify(answers, null, '  '));
  });